<?php namespace App\Http\Controllers;

use App\Banner;
use App\Classify;
use App\Http\Services\FileManager;
use Illuminate\Http\Request;
use App\Content;
use Cookie;


class HomeController extends Controller
{

    public function getUserToken(Request $request){

            echo Cookie::get('user_token');

    }

    public function contentList(Request $request)
    {
        $classify = !is_null($request->get('classify')) ? $request->get('classify') : 0;
        $thatClassify = Classify::whereId($classify)->first();
        if (!$thatClassify)
            return response()->json(["level" => "error", "message" => "当前分类不存在", "data" => ""]);

        $contents = Content::whereClassifyId($thatClassify->id)->orderBy("created_at", "desc")->get();

        if (count($contents) > 0) {
            foreach ($contents as $content) {
                /** @var Content $content */
                $content->img_url = FileManager::getAliOssFile($content->img_id);
            }
        }

        $banners = Banner::whereClassifyId($classify)->get();
        if (count($banners) > 0) {
            foreach ($banners as $banner) {
                $banner->img_url = FileManager::getAliOssFile($banner->img_id);
            }
        }

        return view("content", ['contents' => $contents, "banners" => $banners]);
//        return view("content");
    }

    public function memberRegister(Request $request){
        return view('member.index');
    }

    public function checkCode(Request $request){
        return view('member.check');
    }
}